var url = "http://127.0.0.1:8083/";

function getCookie(key){
    var cookie = document.cookie;
    var s = cookie.replace("; ","=");
    while (s.indexOf(";") != -1){
        s = s.replace("; ","=");
    }
    var strings = s.replace("; ","=").split("=");
    if (strings.indexOf(key) == -1){
        return null;
    }
    return strings[strings.indexOf(key) + 1].trim();
}

window.onload=function () {

    $(function () {
        $.ajax({
            url: url + "order/getOrderList",
            type: "POST",
            data: {userName: getCookie("USER_NAME")},
            success: function (data) {
                if (data["code"] == 200) {
                    var datum = data["data"];
                    var $mycart = $(".mycart .container .row .col-md-12");
                    for (let i = 0; i < datum.length; i++) {
                        var tr = "";
                        var orderItems = datum[i]["orderItems"];
                        var state = "        <a href=\"javascript:void(0);\" class=\"btn\" onclick='comment(this)'>\n" +
                                    "            <i class=\"fa fa-pencil-square-o\">评价</i>\n" +
                                    "        </a>\n";
                        if (datum[i]["stateId"] == 1){
                            state = "        <a href=\"javascript:void(0);\" class=\"btn\" onclick='pay(this)'>\n" +
                                    "            <i class=\"fa fa-credit-card-alt\">付款</i>\n" +
                                    "        </a>\n";
                        }else if (datum[i]["stateId"] == 2) {
                            state = "        <a href=\"javascript:void(0);\" class=\"btn\">\n" +
                                    "            待发货\n" +
                                    "        </a>\n";
                        }else if (datum[i]["stateId"] == 3) {
                            state = "        <a href=\"javascript:void(0);\" class=\"btn\" onclick='sure(this)'>\n" +
                                    "            <i class=\"fa hand-pointer-o\">确认收货</i>\n" +
                                    "        </a>\n";
                        }else if (datum[i]["stateId"] == 5){
                            state = "            订单完成\n"
                        }
                        for (let j = 0; j < orderItems.length; j++) {
                            var productPo = orderItems[j]["productPo"];
                            tr +=
                                "<tr>\n" +
                                "    <td>\n" +
                                "        <div class=\"center\">\n" +
                                "            <input type=\"hidden\" class=\"itemId\" value=\"" + orderItems[j]["itemId"] + "\"/>\n" +
                                "            <img src=\"" + productPo["productPicture"] + "\" alt=\"\">\n" +
                                "        </div>\n" +
                                "    </td>\n" +
                                "    <td>" + productPo["productName"] + "</td>\n" +
                                "    <td>￥ <span class=\"unit-price\">" + productPo["productPrice"] + "</span> </td>\n" +
                                "    <td>\n" +
                                "        <p class=\"form-control-plaintext\">" + orderItems[j]["total"] + "</p>\n" +
                                "    </td>\n" +
                                "    <td>\n" +
                                "        ￥<span class=\"sum-amount\">" + orderItems[j]["orderCount"] + "</span>\n" +
                                "    </td>\n" +
                                "    <td>\n" + state +
                                "        <a href=\"javascript:void(0);\" class=\"btn\">\n" +
                                "            <i class=\"fa fa-trash\">删除</i>\n" +
                                "        </a>\n" +
                                "    </td>\n" +
                                "</tr>\n";
                        }
                        $mycart.append(
                            "<div class=\"card\">\n" +
                            "    <div class=\"card-header bg-light\">\n" +
                            "        订单号： <span class='order-id'>" + datum[i]["orderId"] + "</span>\n" +
                            "    </div>\n" +
                            "    <div class=\"card-body\">\n" +
                            "        <div class=\"table-responsive\">\n" +
                            "            <table class=\"table table-hover\">\n" +
                            "                <thead>\n" +
                            "                <tr>\n" +
                            "                    <th width=\"200\"></th>\n" +
                            "                    <th width=\"320\"></th>\n" +
                            "                    <th width=\"130\"></th>\n" +
                            "                    <th width=\"220\"></th>\n" +
                            "                    <th width=\"130\"></th>\n" +
                            "                    <th width=\"120\"></th>\n" +
                            "                </tr>\n" +
                            "                </thead>\n" +
                            "                <tbody>\n" +
                            "                    <tr>\n" + tr +
                            "                </tbody>\n" +
                            "            </table>\n" +
                            "        </div>\n" +
                            "    </div>\n" +
                            "</div>"
                        );
                    }
                }else {
                    window.location.href=url + "user/" + data["data"] + ".html";
                }
            }
        })
    })
};

//评价
function comment(obj) {
    var orderId = $(obj).parent().parent().parent().parent().parent().parent().prev().find(".order-id");
    $.ajax({
        url: url + "order/comment",
        type: "POST",
        data: {orderId: orderId.text()},
        success: function (data) {
            if (data["code"] == 200) {
                window.location.href=url + "user/user_product_details.html";
            }
        }
    });
}

//支付
function pay(obj) {
    var orderId = $(obj).parent().parent().parent().parent().parent().parent().prev().find(".order-id");
    $.ajax({
        url: url + "order/AliPay",
        type: "POST",
        data: {orderId: orderId.text()},
        success: function (data) {
            document.write(data);
        }
    });
}

//确认收货
function sure(obj){
    var orderId = $(obj).parent().parent().parent().parent().parent().parent().prev().find(".order-id");
    $.ajax({
        url: url + "order/sure",
        type: "POST",
        data: {orderId: orderId.text()},
        success: function (data) {
            if (data["code"] == 200) {
                alert("确认收货成功");
            }
        }
    });
}